<?php

namespace Acme\FirmUsersBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Acme\FirmUsersBundle\Entity\User;
use Acme\FirmUsersBundle\Form\UserType;
use Acme\FirmUsersBundle\lib\AdminConfigJqGrid;
use JMS\SecurityExtraBundle\Annotation\Secure;

/**
 * User controller.
 *
 * @Route("/admin/admin_user")
 */
class AdminController extends Controller {

  /**
   * Lists all User entities.
   *
   * @Route("/", name="admin_user")
   * @Template()
   */
  public function indexAction() {
    $em = $this->getDoctrine()->getEntityManager();
    //
    //        $entities = $em->getRepository('AcmeFirmUsersBundle:User')->findAll();

    $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                'Repository' => 'AcmeFirmUsersBundle:User'));
    $adconfig->getJqGrid()->findByKey('url')
             ->setValue($this->generateUrl('_get_user'));
    $adconfig->getJqGrid()->findByKey('editurl')
                          ->setValue($this->generateUrl('_del_user'));
    
    return array('params' => $adconfig->getJqGrid()->getProperties());
  }

  /**
   *
   * @Route("/get_user", name="_get_user") 
   */
  public function get_userAction(\Symfony\Component\HttpFoundation\Request $request) {

    if ($request->isXmlHttpRequest()) {
      $em = $this->getDoctrine()->getEntityManager();
      $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                                              'Repository'    => 'AcmeFirmUsersBundle:User'));
      $adconfig->getJqGrid()->findByKey('url')
               ->setValue($this->generateUrl('_get_user'));
      $adconfig->getJqGrid()->findByKey('editurl')
                            ->setValue($this->generateUrl('_del_user'));
      $gj = new \Alex616\jqGridBundle\lib\GeneratorJqGrid($adconfig);
      $results = $gj->getResults($request, 'p');
      $response = new \Symfony\Component\HttpFoundation\Response($results, 200, array('Content-Type' => 'application/json'));
      return $response;
    }
    return new \Symfony\Component\HttpFoundation\Response('', 404);
  }

  /**
   * Finds and displays a User entity.
   *
   * @Route("/{id}/show", name="admin_user_show")
   * @Template()
   */
  public function showAction($id) {
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('AcmeFirmUsersBundle:User')->find($id);

    if (!$entity) {
      throw $this->createNotFoundException('Unable to find User entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity' => $entity,
        'delete_form' => $deleteForm->createView(),);
  }

  /**
   * Displays a form to create a new User entity.
   *
   * @Route("/new", name="admin_user_new")
   * @Template()
   */
  public function newAction() {
    $entity = new User();
    $form = $this->createForm(new \Acme\FirmUsersBundle\Form\AdminUserForm(), $entity);

    return array(
        'entity' => $entity,
        'form' => $form->createView()
    );
  }

  /**
   * Creates a new User entity.
   *
   * @Route("/create", name="admin_user_create")
   * @Method("post")
   * @Template("AcmeFirmUsersBundle:User:new.html.twig")
   */
  public function createAction() {
    $entity = new User();
    $request = $this->getRequest();
    $form = $this->createForm(new \Acme\FirmUsersBundle\Form\AdminUserForm(), $entity);
    $form->bindRequest($request);

    if ($form->isValid()) {
      $em = $this->getDoctrine()->getEntityManager();
      $em->persist($entity);
      $em->flush();

      return $this->redirect($this->generateUrl('admin_user', array('id' => $entity->getId())));
    }

    return array(
        'entity' => $entity,
        'form' => $form->createView()
    );
  }

  /**
   * Displays a form to edit an existing User entity.
   *
   * @Route("/{id}/edit", name="admin_user_edit")
   * @Template()
   */
  public function editAction($id) {
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('AcmeFirmUsersBundle:User')->find($id);

    if (!$entity) {
      throw $this->createNotFoundException('Unable to find User entity.');
    }

    $editForm = $this->createForm(new \Acme\FirmUsersBundle\Form\AdminEditUserForm(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
  }

  /**
   * Edits an existing User entity.
   *
   * @Route("/{id}/update", name="admin_user_update")
   * @Method("post")
   * @Template("AcmeFirmUsersBundle:Admin:edit.html.twig")
   */
  public function updateAction($id) {
    $em = $this->getDoctrine()->getEntityManager();

    $entity = $em->getRepository('AcmeFirmUsersBundle:User')->find($id);

    if (!$entity) {
      throw $this->createNotFoundException('Unable to find User entity.');
    }

    $editForm = $this->createForm(new \Acme\FirmUsersBundle\Form\AdminEditUserForm(), $entity);
    $deleteForm = $this->createDeleteForm($id);

    $request = $this->getRequest();

    $editForm->bindRequest($request);

    if ($editForm->isValid()) {
      $em->persist($entity);
      $em->flush();

      return $this->redirect($this->generateUrl('admin_user_edit', array('id' => $id)));
    }

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    );
  }

  /**
   * Deletes a User entity.
   *
   * @Route("/{id}/delete", name="admin_user_delete")
   * @Method("post")
   */
  public function deleteAction($id) {
    $form = $this->createDeleteForm($id);
    $request = $this->getRequest();

    $form->bindRequest($request);

    if ($form->isValid()) {
      $em = $this->getDoctrine()->getEntityManager();
      $entity = $em->getRepository('AcmeFirmUsersBundle:User')->find($id);

      if (!$entity) {
        throw $this->createNotFoundException('Unable to find User entity.');
      }

      $em->remove($entity);
      $em->flush();
    }

    return $this->redirect($this->generateUrl('admin_user'));
  }

  private function createDeleteForm($id) {
    return $this->createFormBuilder(array('id' => $id))
                    ->add('id', 'hidden')
                    ->getForm()
    ;
  }
  /**
   * @Route("/del_user", name="_del_user")
   * 
   */
  public function deleteUserAction(\Symfony\Component\HttpFoundation\Request $request) {
    if ( $request->isXmlHttpRequest() ) {
      $em = $this->getDoctrine()->getEntityManager();
      $adconfig = new AdminConfigJqGrid(array('EntityManager' => $em,
                                              'Repository'    => 'AcmeFirmUsersBundle:User'));
      $adconfig->getJqGrid()->findByKey('url')
              ->setValue($this->generateUrl('_get_user'));
      $adconfig->getJqGrid()->findByKey('editurl')
                            ->setValue($this->generateUrl('_del_user'));
      $gj = new \Alex616\jqGridBundle\lib\GeneratorJqGrid($adconfig);
      $gj->removeObjects($request);
    }
    return new \Symfony\Component\HttpFoundation\Response('', 200);
  }

}